<template>
  <div class="card-container">
    <el-form :model="form" :rules="formRules" ref="formRef" label-width="120px" style="width: 100%;" label-position="right">
      <el-card class="box-card" shadow="never">
        <div slot="header" class="clearfix">
          <span>接口信息</span>
        </div>
      </el-card>
      <el-form-item>
        <el-button type="primary" @click="submitForm">立即添加</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addApi, getCList, getMethod } from '@/api/permission'

export default {
  name: 'addAdviser',
  data() {
    return {
      form: {
        pid: '',
        level: 2,
        right: [],
      },
      formRules: {
        right: [{ required: true, message: '请选择接口权限', trigger: 'change' }],
      },
      cList: [],
    }
  },
  created() {
    this.getCList()
  },
  methods: {
    async getCList() {
      const res = await getCList()
      console.log(res)
      this.cList = res.data
    },
    handleChange(val) {
      this.form.province_id = val[0]
      this.form.city_id = val[1]
      this.form.district_id = val[2]
      console.log(val)
    },
    uploadImg(type, pic) {
      this.form[type] = pic
    },
    removeImg(type) {
      this.form[type] = ''
    },
    handleCityData() {
      this.cityData.forEach(x => {
        if (this.form.province_id == x.value) {
          this.form.province = x.label
        }
        x.children.forEach(y => {
          if (this.form.city_id == y.value) {
            this.form.city = y.label
          }
          y.children.forEach(z => {
            if (this.form.district_id == z.value) {
              this.form.district = z.label
            }
          })
        })
      })
    },
    async submitForm() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return false
        this.handleCityData()
        const res = await addAdviser(this.form)
        if (res.status != 1) return this.$message.error(res.msg)
        this.$message.success(res.msg)
        this.closePage()
      })
    },
    resetForm() {
      this.$refs.formRef.resetFields()
      this.form = this.$options.data().form
    },
    async closePage() {
      let view = this.$route
      await this.$store.dispatch('tagsBar/delRoute', view)
      this.$router.push('/adviserList')
    },
  },
}
</script>

<style lang="scss" scoped>
// .el-form-item {
//   // width: 420px;
// }
::v-deep .el-input__inner {
  // margin-left: 20px;
  height: 36px;
  width: 420px;
}
::v-deep .el-checkbox {
  height: auto !important;
}
.el-card {
  border-radius: 8px;
}
::v-deep .el-card__header {
  font-size: 14px;
  color: #555;
  font-weight: bold;
}
::v-deep .el-form-item__label {
  padding-right: 12px;
}
::v-deep .el-range-separator {
  line-height: 28px;
}
::v-deep .el-range-input {
  cursor: pointer;
}
.radio-box {
  display: flex;
  align-items: center;
  height: 36px;
  width: 600px;
}
.tips {
  color: #999;
  margin-top: 10px;
  font-size: 12px;
}
</style>
